package ua.tach.shortl.screens;

import ua.tach.shortl.MainInput;
import ua.tach.shortl.TextureLoader;
import ua.tach.shortl.logic.Country;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Files.FileType;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.BoundingBox;

public class MainMenu implements Screen {
	public static final BoundingBox SOUND_YES = new BoundingBox(new Vector3(94, 130, 0), new Vector3(402,420,0));
	public static final BoundingBox SOUND_NO = new BoundingBox(new Vector3(410, 130, 0), new Vector3(740, 420, 0));
	private final SpriteBatch spriteBatch;
	/** is done flag **/
	private boolean isDone = false;
	private boolean isSound = true;
	private final MainInput input;
	/** view & transform matrix **/
	private final Matrix4 viewMatrix = new Matrix4();
	private float accum;

	public MainMenu (Application app) {
		Country.menuWait = 0;
		input = new MainInput();
		spriteBatch = new SpriteBatch();
		viewMatrix.setToOrtho2D(0, 0, 800, 480);
		Gdx.input.setInputProcessor(input);
	}

	@Override public void render (Application app) {
		GL10 gl = app.getGraphics().getGL10();
		gl.glViewport(0,0,800,480);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

		accum += app.getGraphics().getDeltaTime();
		//System.out.println("acc "+accum);
		spriteBatch.setProjectionMatrix(viewMatrix);
		spriteBatch.begin();
		spriteBatch.draw(TextureLoader.background, 0, 0);
		if (accum>6) {
			accum = 0;
			if (++Country.menuWait > 4 && !isSound) isDone = true;
		}else if (accum > 4)
			spriteBatch.draw(TextureLoader.logo2, 152, 400);
		else if(accum > 2)
			spriteBatch.draw(TextureLoader.logo1, 152,400);

		spriteBatch.end();
	}

	@Override public void update (Application app) {
		if (input.inp.x == -1) return;
		if (isSound) {
			if(SOUND_YES.contains(input.inp)) {
				System.out.println("Sound yes! ");
				Country.enableSound = true;
				if (Country.intro == null) {
					Country.intro = app.getAudio().newMusic(app.getFiles().getFileHandle("data/sl_country.mp3", FileType.Internal));
					Country.intro.setLooping(true);
				}
				Country.intro.play();
				isSound = false;
			} else if (SOUND_NO.contains(input.inp)) {
				System.out.println("Sound no! ");
				Country.enableSound = false;
				isSound = false;
			}
		} else {
			System.out.println("Touch! ");
			isDone = true;
		}
		input.inp.x = -1;
	}

	@Override public boolean isDone () {
		return isDone;
	}

	@Override public void dispose () {
		spriteBatch.dispose();
	}
}
